package com.ocom.payconfig.controller;

import com.ocom.common.constant.PayConfigUrl;
import com.ocom.common.entity.payconfig.CouponsItem;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.DateUtil;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.payconfig.CouponsItemVo;
import com.ocom.common.vo.payconfig.GrantCouponsVo;
import com.ocom.payconfig.service.CouponsItemService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.text.ParseException;


/**
 * 赠券项目
 */
@Slf4j
@RestController
@AllArgsConstructor
public class CouponsItemController {
    private final CouponsItemService couponsItemService;

    @PostMapping(PayConfigUrl.Url_updateCouponsItemById)
    public Result updateCouponsItemById(@RequestParam("comId")Long comId,@RequestParam("itemId") int item_id, @RequestParam("status")int status, @RequestParam("breakTime")String breakTime) {
        return ResultUtil.success(couponsItemService.updateCouponsItemById(comId,item_id,status,breakTime));
    }

    /**
     * 添加赠券发放批次
     */
    @PostMapping(PayConfigUrl.Url_newCouponsItem)
    public Result newCouponsItem(@RequestBody CouponsItem couponsItem) {
        if(couponsItem.getStartDate().after(couponsItem.getStopDate())){
            return ResultUtil.error(ResultEnum.Err_4207);
        }
        CouponsItem coupon = couponsItemService.selectCouponsItemByName(Long.valueOf(couponsItem.getComId()),couponsItem.getItemName());
        if(coupon != null){
            return ResultUtil.error(ResultEnum.Err_4123);
        }else{
            return ResultUtil.success(couponsItemService.save(couponsItem));
        }
    }

    /**
     * 赠券项目列表
     */
    @PostMapping(PayConfigUrl.Url_couponsItem)
    public Result couponsItem(@RequestBody CouponsItemVo params) {
        //查询列表数据
//        QueryWrapper<CouponsItem> queryWrapper = new QueryWrapper<>();
//        IPage<CouponsItem> listPage = couponsItemService.page(mpPageConvert.<CouponsItem>pageParamConvert(params), queryWrapper);
        Result list = couponsItemService.couponsItem(params.getComId(),params.getPage(),params.getSize());
//        System.out.println("list===" + list.size());
        return list;
    }

    /**
     * 13.21更新赠券发放批次
     * @param couponsItem
     * @return
     */
    @PostMapping(PayConfigUrl.Url_updateCouponsItem)
    public Result updateCouponsItem(@RequestBody CouponsItem couponsItem) {
        return ResultUtil.success(couponsItemService.CouponsItemUpdateById(Long.valueOf(couponsItem.getComId()),couponsItem));
    }

//    @DeleteMapping("/delete")
//    public Result delete(@RequestBody CouponsItem couponsItem) {
//        couponsItemService.removeById(couponsItem.getItemId());
//        return ResultUtil.success();
//    }

//    /**
//     * 13.19停用过期赠券
//     * @return
//     */
//    @PostMapping(PayConfigUrl.Url_stopCoupons)
//    public Result stopCoupons(){
//        couponsItemService.selectCouponsItemByStop(1L,1);
//        log.info("定时任务处理 停用过期赠券:处理库一完成】");
//        boolean bReturn = couponsItemService.selectCouponsItemByStop(600L,1);
//        log.info("定时任务处理 停用过期赠券:处理库二完成】");
//        return ResultUtil.success(bReturn);
//    }

    @PostMapping(PayConfigUrl.Url_CouponsVoucherTimerStop)
    public Result couponsVoucherTimerStop(@RequestParam("startComId") Long startComId){
        return ResultUtil.success(couponsItemService.couponsItemByStop(startComId,1));
    }

    @PostMapping(PayConfigUrl.Url_grantCoupons)
    public Result grantCoupons(@RequestBody @Valid GrantCouponsVo request){
        try {
            log.info("payConfig 发放赠券 开始有效日期: " + DateUtil.getDate(request.getStopDate(),"yyyy-MM-dd HH:mm:ss"));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return couponsItemService.grantCoupons(request.getComId(),request);
    }

}
